package com.creditdnepr.freebank.utils;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.*;
import org.json.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;
import android.widget.Toast;

import com.creditdnepr.freebank.FreeBank;
import com.creditdnepr.freebank.data.CurrencyRates;

public class RtsServer {
	
    private static HttpClient getHttpClient()
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {
            public boolean retryRequest(IOException ioexception, int i, HttpContext httpcontext) {
                return (i < 4);
            }
        };
        client.setHttpRequestRetryHandler(handler);
        return client;
    }
    
    private HttpResponse execGetRequest(String url) throws IOException {
    	HttpGet httpget = new HttpGet(url);
        return getHttpClient().execute(httpget);
    }
    
    private HttpResponse execPostRequest(String url) throws IOException {
    	HttpPost httppost = new HttpPost(url);
    	return getHttpClient().execute(httppost);
    }
    
    private Document parseXml(InputStream inputstream)
            throws ParserConfigurationException, IOException, SAXException {
            DocumentBuilder documentbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource inputsource = new InputSource(inputstream);
            inputsource.setEncoding("cp1251");
            return documentbuilder.parse(inputsource);
        }
    
    public CurrencyRates getRates() throws IllegalStateException, IOException, ParserConfigurationException, SAXException {
    	CurrencyRates rates = new CurrencyRates();
    	InputStream inputstream = execGetRequest(Constants.cRatesUrl).getEntity().getContent();
    	Document document = parseXml(inputstream);
    	
    	NodeList nodelist = document.getElementsByTagName("item");
    	if (nodelist.getLength() > 0) {
    		if (((Document)nodelist.item(0)).getElementsByTagName("title").getLength() > 0) {
    			String s = ((Document)nodelist.item(0)).getElementsByTagName("title").item(0).getTextContent();
    			rates.setRssTitle(s);
    			Log.i("FreeBank", rates.getRssTitle());
    		}
    		if (((Document)nodelist.item(0)).getElementsByTagName("description").getLength() > 0) {
    			String s = ((Document)nodelist.item(0)).getElementsByTagName("description").item(0).getTextContent();
        		Log.i("FreeBank", s);
    		}
    	}
    	return rates;
    }

	public RtsServer() {
	}

}
